<?php

namespace ImiApp\ApiServer\Controller;

use Imi\App;
use Imi\Db\Db;
use Amis\type\Api;
use Amis\other\Tpl;
use Imi\AppContexts;
use Amis\layout\Page;
use Amis\type\AmisEcho;
use Amis\functions\Nav;
use Amis\form\InputText;
use Amis\dataviewer\CRUD;
use Amis\functions\Button;
use Amis\functions\AmisApp;
use Amis\other\Avatar;
use Amis\functions\NavLinks;
use Amis\dataviewer\TableView;
use Amis\functions\AmisAppPages;
use Amis\functions\DropDownButton;
use Imi\Util\Http\Consts\MediaType;
use Imi\Server\Http\Message\Response;
use Imi\Util\Http\Consts\ResponseHeader;
use ImiApp\ApiServer\Common\BasetController;
use Imi\Server\Http\Controller\HttpController;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Imi\Server\View\Annotation\HtmlView;
use Imi\Server\View\Annotation\View;


/**
 * @Controller ("/")
 * @HtmlView(baseDir="index/")
 */
class IndexController extends BasetController
{
    /**
     * @Action
     * @Route("/")
     * @View(renderType="json")
     *
     * @return array
     */
    public function index()
    {

        return [
            'classNames' => Avatar::Instance()->className('this is class')->getData(),
            'style'      => Avatar::Instance()->style('this is style')->getData(),
            'hello'      => 'imi',
            'time'       => date('Y-m-d H:i:s', time()),
        ];
    }

    /**
     * @Action
     * @Route("/logos")
     * @View(renderType="html")
     * @return int
     */
    public function logos()
    {

        return $this->getImg('/amis-admin/public/logo.png');
    }

    /**
     * @Action
     * @Route("/api")
     * @return array
     */
    public function api()
    {
        return [
            'mode'  => App::getApp()->getType(),
            'hello' => 'imi-api',
            'time'  => date('Y-m-d H:i:s', time()),
        ];
    }

    /**
     * @Action
     * @return array
     */
    public function db()
    {
        return [
            'mode'         => App::getApp()->getType(),
            'mysqlVersion' => Db::getInstance()->query('select VERSION()')->fetchColumn(),
        ];
    }

    /**
     * @Action
     * @Route("/nav")
     * @View(renderType="json")
     * @return array
     */
    public function nav()
    {
        return [
            ['label' => '系统', 'to' => ''],
            ['label' => '系统', 'to' => ''],
            ['label' => '系统', 'to' => ''],
            ['label' => '系统', 'to' => ''],
            ['label' => '系统', 'to' => ''],
        ];
    }

    /**
     * @Action
     * @Route("/amis")
     * @View(renderType="html")
     * @return array
     */
    public function amis()
    {
        return AmisApp::Instance()
            ->api('/pages/site')
            ->brandName('Amis Admin')
            ->logo('/logos')
            ->header(TableView::Instance()->tableBorder(false)->trs([
                TableView::Instance()->tds([
                    TableView::Instance()->tdsAlign()->body(Nav::Instance()->source('/nav')),
                    TableView::Instance()->tdsAlign()
                        ->tdsWidth(200)
                        ->body([
                            DropDownButton::Instance()->label('Admin')
                                ->buttons([
                                    Button::Instance()->label('个人资料')->disabled(true),
                                    Button::Instance()->label('修改密码')->disabled(true),
                                    Button::Instance()->label('退出登录')->disabled(true),
                                ]),
                            Avatar::Instance()->icon()->getData(),
                        ]),
                ])->getData(),
            ])->getData())
//            ->footer(Tpl::Instance()->inline(false)->className('w-full')->tpl('底部区域'))
            ->getData();
    }


    /**
     * @Action
     * @Route("/pages/site")
     * @View(renderType="json")
     */
    public function site()
    {
        return AmisEcho::Instance()->status()->msg()->data([
            AmisAppPages::Instance()->pages([
                AmisAppPages::Instance()->label('Home')->url('/')->redirect('/crud'),
                AmisAppPages::Instance()->label('列表示例')->url('/crud')
                    ->rewrite('/crud/list')->icon('fa fa-cube')
                    ->children([
                        AmisAppPages::Instance()->label('列表')->url('/crud/list')
                            ->icon('fa fa-list')->schemaApi('get:/pages/crud-list'),
                    ]),
            ]),
        ]);
    }

    public function page()
    {

    }

    /**
     * @Action
     * @Route("/pages/crud-lists")
     * @View(renderType="json")
     */
    public function curd_lists()
    {
        Page::Instance()->title('数据列表')->remark();
    }

    /**
     * @Action
     * @Route("/pages/crud-list")
     * @View(renderType="json")
     */
    public function curd_list()
    {
        Page::Instance()->title('数据列表')->toolbar([
            \Amis\functions\Action::Instance()->actionType('link')->label('新增'),
        ])->body([
            CRUD::Instance()->title('用户数据表')
            ->api('https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/sample')
            ->filter([
                'title'=>'搜索',
                "mode"          => "inline",
                "wrapWithPanel" => false,
                'body'=>[
                    InputText::Instance()->name('keywords')->label('关键字搜索')->getData()
                ]
            ])->bulkActions()
        ]);


        return [
            "type"    => "page",
            "title"   => "列表",
            "remark"  => null,
            "name"    => "page-demo",
            "toolbar" => [
                [
                    "type"       => "button",
                    "actionType" => "link",
                    "link"       => "/crud/new",
                    "label"      => "新增",
                    "primary"    => true,
                ],
            ],
            "body"    => [
                [
                    "type"             => "crud",
                    "name"             => "sample",
                    "api"              => "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/sample",
                    "filter"           => [
                        "title"         => "",
                        "mode"          => "inline",
                        "wrapWithPanel" => false,
                        "submitText"    => "",
                        "controls"      => [
                            [
                                "type"        => "text",
                                "name"        => "keywords",
                                "placeholder" => "通过关键字搜索",
                                "addOn"       => [
                                    "label"     => "搜索",
                                    "type"      => "submit",
                                    "className" => "btn-success",
                                ],
                                "clearable"   => true,
                            ],
                        ],
                        "className"     => "m-b-sm",
                    ],
                    "bulkActions"      => [
                        [
                            "label"      => "批量修改",
                            "type"       => "button",
                            "actionType" => "dialog",
                            "level"      => "primary",
                            "dialog"     => [
                                "title" => "批量编辑",
                                "name"  => "sample-bulk-edit",
                                "body"  => [
                                    "type"     => "form",
                                    "api"      => "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/sample/bulkUpdate2",
                                    "controls" => [
                                        [
                                            "type"  => "text",
                                            "name"  => "engine",
                                            "label" => "Engine",
                                        ],
                                    ],
                                ],
                            ],
                        ],
                        [
                            "label"       => "批量删除",
                            "type"        => "button",
                            "level"       => "danger",
                            "actionType"  => "ajax",
                            "api"         => "delete:https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/sample/\$ids",
                            "confirmText" => "确定要批量删除?",
                        ],
                    ],
                    "columns"          => [
                        [
                            "name"     => "engine",
                            "label"    => "Rendering engine",
                            "sortable" => true,
                        ],
                        [
                            "name"     => "id",
                            "label"    => "ID",
                            "width"    => 20,
                            "sortable" => true,
                        ],
                        [
                            "name"     => "browser",
                            "label"    => "Browser",
                            "sortable" => true,
                        ],
                        [
                            "name"     => "platform",
                            "label"    => "Platform(s)",
                            "sortable" => true,
                        ],
                        [
                            "name"  => "version",
                            "label" => "Engine version",
                        ],
                        [
                            "name"  => "grade",
                            "label" => "CSS grade",
                        ],
                        [
                            "type"        => "operation",
                            "label"       => "操作",
                            "width"       => "",
                            "buttons"     => [
                                [
                                    "type"    => "button-group",
                                    "buttons" => [
                                        [
                                            "type"       => "button",
                                            "label"      => "查看",
                                            "level"      => "primary",
                                            "actionType" => "link",
                                            "link"       => "/crud/\$id",
                                        ],
                                        [
                                            "type"       => "button",
                                            "label"      => "修改",
                                            "level"      => "info",
                                            "actionType" => "link",
                                            "link"       => "/crud/\$id/edit",
                                        ],
                                        [
                                            "type"        => "button",
                                            "label"       => "删除",
                                            "level"       => "danger",
                                            "actionType"  => "ajax",
                                            "confirmText" => "您确认要删除?",
                                            "api"         => "delete:https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/sample/\$id",
                                        ],
                                    ],
                                ],
                            ],
                            "placeholder" => "-",
                            "fixed"       => "right",
                        ],
                    ],
                    "affixHeader"      => true,
                    "columnsTogglable" => "auto",
                    "placeholder"      => "暂无数据",
                    "tableClassName"   => "table-db table-striped",
                    "headerClassName"  => "crud-table-header",
                    "footerClassName"  => "crud-table-footer",
                    "toolbarClassName" => "crud-table-toolbar",
                    "combineNum"       => 0,
                    "bodyClassName"    => "panel-default",
                ],
            ],
        ];
    }
}
